<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\FangAttrs;
use Illuminate\Http\Request;
use OSS\OssClient;
use OSS\Core\OssException;

class FangAttrController extends BaseController
{
    /**
     * 房源属性展示页
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $data = (new FangAttrs())->getAll();
        return view('admin.fangattr.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $data = (new FangAttrs())->getAll();
        return view('admin.fangattr.create',compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $params = $request->except('_token');
        $res = FangAttrs::create($params);

        if($res){
            return  redirect(route('admin.fangattr.index'));
        }else{
            return  redirect(route('admin.fangattr.create'))->withErrors('添加失败');
    }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 上传文件到云端
     */
    public function ossfile(Request $request)
    {
        $filePath = $request->file('file')->getPathname();

        // 阿里云账号AccessKey拥有所有API的访问权限
        $accessKeyId = "LTAI5tKGdFnsYGHudzGYfeN8";

        $accessKeySecret = "1UxHYX4fDgyOR4IVxL2q1LCNFIXzzd";
        //填写Bucket所在地域
        $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        //桶名称
        $bucket= "cyf2008a";
        // 储存在桶的路径
        $object = "fang_atrr/".md5(rand(1,5).time()).'.png';
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $ossClient->uploadFile($bucket,$object,$filePath);

            $iconPath = 'https://cyf2008a.oss-cn-shanghai.aliyuncs.com/'.$object;

            return $this->witeJson(0,['icon'=>$iconPath],'上传成功');


        } catch (OssException $e) {
            return $this->witeJson(10001,'','上传失败');
        }

    }
}
